"""
    Software : PyCharm
    File     : serializers.py
    Time     : 2021/5/24 18:13
    Author   : Pontoon
    describe :
"""
from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

from .models import UserProfile


class TokenLoginSerializer(TokenObtainPairSerializer):
    """" 继承TokenObtainPairSerializer """
    def validate(self, attrs):
        """
        重写 validate 方法
        :param attrs:
        :return:
        """
        _data = {"token": {}}

        # TokenObtainPairSerializer 类中完成了登录校验功能, 所以可以直接通过self.user取到已登录的用户信息
        data = super().validate(attrs)

        _data['token']['refresh'] = data['refresh']
        _data['token']['access'] = data['access']

        _data["nickname"] = self.user.nickname
        _data["uid"] = self.user.uid
        _data["avatar"] = self.user.avatar

        return _data


class ChatUserSerializer(serializers.ModelSerializer):
    # 如何判断用户在线状态
    class Meta:
        model = UserProfile
        fields = ['uid', 'nickname', 'avatar']


